<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <page-tools>
          <!-- 插入到left插槽位置 -->
          <template #left>
            <span>本月: 社保在缴 公积金在缴</span>
          </template>
          <!-- 插入到right插槽位置 -->
          <template #right>
            <!--  v-if="
                $store.state.user.userInfo.roles.points.includes('import_excel')
              " -->
            <el-button
              v-allow="'import_excel'"
              type="warning"
              size="small"
              @click="$router.push('/import')"
            >导入excel</el-button>
            <!-- v-if="
                $store.state.user.userInfo.roles.points.includes('exprot_excel')
              " -->
            <el-button
              v-allow="'exprot_excel'"
              type="danger"
              size="small"
              @click="handleDownload"
            >导出excel</el-button>
            <el-button
              type="primary"
              size="small"
              @click.native="addEmployee"
            >新增员工</el-button>
          </template>
        </page-tools>
        <el-card style="margin-top: 10px">
          <el-table border :data="list" :default-sort="{ prop: 'workNumber' }">
            <el-table-column type="index" label="序号" />
            <el-table-column label="姓名" prop="username" />
            <el-table-column label="图片" prop="staffPhoto">
              <template slot-scope="scope">
                <!-- <img
                  :src="scope.row.staffPhoto"
                  style="width: 100%; height: 100%"
                > -->
                <ImageHolder :src="scope.row.staffPhoto" />
              </template>
            </el-table-column>
            <el-table-column label="工号" prop="workNumber" />
            <el-table-column label="聘用形式" prop="formOfEmployment">
              <template slot-scope="scope">
                <!-- <button @click="f(scope)">正式</button> -->
                <!-- {{ scope.row.formOfEmployment === 1 ? '正式' : '非正式' }} -->
                {{ format(scope.row.formOfEmployment) }}
              </template>
            </el-table-column>
            <el-table-column label="部门" prop="departmentName" />
            <el-table-column label="入职时间" prop="timeOfEntry" sortable />
            <el-table-column label="操作" width="280">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="$router.push('/employees/detail?id=' + scope.row.id)"
                >查看</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="AssignRoles(scope.row.id)"
                >分配角色</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="doDel(scope.row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <el-pagination
              :total="total"
              :current-page="page"
              :page-sizes="[1, 2, 5, 10]"
              :page-size="pagesize"
              layout="prev,pager,next,sizes"
              @size-change="hSizeChange"
              @current-change="hCurrentChange"
            />
          </el-row>
        </el-card>
        <!-- <Student /> -->
      </el-card>
      <el-dialog
        title="添加部门"
        :visible.sync="showDialog"
        :close-on-click-modal="true"
        :close-on-press-escape="true"
        @close="hCloseDialog"
      >
        <EmpDialog ref="AddEmpDialog" :list="list" @success="hSuccess" />
      </el-dialog>
      <el-dialog
        title="分配权限"
        :close-on-click-modal="true"
        :close-on-press-escape="true"
        :visible.sync="showAssignDialog"
      >
        <AssignRole
          v-if="showAssignDialog"
          :cru-id="cruId"
          @close="showAssignDialog = false"
          @Hsuccess="HRoleSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employee'
// import EmployeeEnum from '@/constant/employees'
// import PageTools from '@/components/PageTools/index.vue'
// const TYPE_MAP = { 1: '正式', 2: '非正式', 3: 'AAA', 4: 'BBB', 5: 'CCC' }
import { TYPE_MAP } from '@/constant/index'
import EmpDialog from './EmpDialog.vue'
import AssignRole from './assignRole.vue'
export default {
  components: { EmpDialog, AssignRole },
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      pagesize: 5,
      showDialog: false,
      showAssignDialog: false,
      cruId: ''
    }
  },
  created() {
    // console.log('employee', this.num)
    // console.log(this.hello())
    this.loadEmployee()
  },
  methods: {
    HRoleSuccess() {
      this.showAssignDialog = false
      this.$message.success('修改成功11')
    },
    AssignRoles(id) {
      this.showAssignDialog = true
      this.cruId = id
    },
    formatExcel(rows) {
      const mapInfo = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      const enheader = Object.keys(rows[0])
      const header = enheader.map((enkey) => mapInfo[enkey])
      const data = rows.map((item) => {
        item.formOfEmployment = TYPE_MAP[item.formOfEmployment]
        return Object.values(item)
      })
      console.log(header + 'header')
      console.log(data + 'data')

      return { header, data }
    },
    // 导出
    async handleDownload() {
      const res = await getEmployeeList({
        page: this.page,
        size: this.pagesize
      })
      const { header, data } = this.formatExcel(res.data.rows)
      import('@/vendor/Export2Excel').then((excel) => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header: header, // 表头 必填
          data: data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    hCloseDialog() {
      this.$refs.AddEmpDialog.resetForm()
    },
    hSuccess() {
      this.total++
      if (this.list.length === this.pagesize) {
        this.page = Math.ceil(this.total / this.pagesize)
      }
      this.showDialog = false
      this.$refs.AddEmpDialog.resetForm()
      this.loadEmployee()
    },
    addEmployee() {
      this.showDialog = true
    },
    async doDelEmployees(id) {
      await delEmployee(id)
      if (this.list.length === 1 && this.page > 1) {
        this.page--
      }
      this.$message.success('删除成功')
      this.loadEmployee()
    },
    doDel(id) {
      this.$confirm('确认删除吗', '提示')
        .then(() => {
          this.doDelEmployees(id)
        })
        .catch(() => {})
    },
    hSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.loadEmployee()
    },
    hCurrentChange(val) {
      this.page = val
      this.loadEmployee()
    },
    format(type) {
      return TYPE_MAP[type] || '未知'
      // if (type === 1) {
      //   return '正式'
      // } else if (type === 2) {
      //   return '非正式'
      // }
    },
    f(scope) {
      console.log(scope)
    },
    async loadEmployee() {
      const res = await getEmployeeList({
        page: this.page,
        size: this.pagesize
      })
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
